package router

import (
	"blog/app/middleware"
	"blog/service"
	"blog/service/local_life_service"
	RateService "blog/service/rate_service"
	"fmt"

	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
)

func Router() *gin.Engine {
	router := gin.Default()

	// 静态资源
	router.Static("/static", "./static")

	// ---------------- 普通路由 ----------------
	router.GET("/", service.Hello)
	router.GET("/user_list", service.GetUserList)
	router.GET("/user_basic_list", service.GetUserBasicList)
	router.POST("/api/admin/login", service.LoginHandler)
	router.POST("/register", service.RegisterHandler)
	router.POST("/new_user", service.CreateUserMongo)
	router.POST("/new_user_list", service.GetUserListFromMongo)
	router.POST("/rand", service.RandUser)

	// ---------- 本地生活API ----------------
	router.GET("/banners", local_life_service.GetBanners)
	router.GET("/grid", local_life_service.GetGridList)

	// -------------------使用路由组和中间件进行鉴权
	auth := router.Group("/auth")
	auth.Use(middleware.JwtToken())
	{
		auth.GET("/user_list", service.GetUserList)
	}

	// --------------------------处理跨域问题
	cors := router.Group("/admin")
	cors.Use(middleware.Cors())
	{
		cors.GET("/users", service.GetUserList)
		cors.OPTIONS("/users", service.GetUserList)

		// 登录接口
		cors.POST("/login", service.LoginHandler)
		cors.OPTIONS("/login", service.LoginHandler)
	}

	// 兼容调试接口 personcard
	// http://localhost:5174/api/personcard/GetTags
	api := router.Group("/api")
	api.Use(middleware.Cors())
	{
		personCard := api.Group("/personcard")

		personCard.Any("/GetTags", service.GetTags)
		// http://localhost:5173/api/personcard/GetUserName
		personCard.Any("/GetUserName", service.GetUserName)
		personCard.Any("/GetUserListByAdminTags", service.GetUserListByAdminTags)

		accessauth := api.Group("/accessauth")
		accessauth.Any("/GetSpecialAuthApplyList", service.GetUserList)
	}

	// 接收数据存储

	// -------------------- 汇率接口--------------------------//
	rateApi := router.Group("/rate")
	rateApi.Use()
	{
		rateApi.GET("/getRate", RateService.GetRateLatest)
		rateApi.GET("/getRatePeriod", RateService.GetRateByDate)

		rateApi.GET("/search", RateService.SearchRateByCode)
		rateApi.GET("/rate_trend", RateService.GetRateTrend)      // 查询一个月一年汇率
		rateApi.GET("/rate_date", RateService.GetRateDate)        // 按开始和结束日期查询汇率
		rateApi.GET("/rate_date_lunar", RateService.GetRateLunar) // 按开始和结束日期查询汇率农历信息

		rateApi.GET("/currency", RateService.CurrencyCode)          // 汇率编码列表
		rateApi.GET("/rate_list", RateService.GetRateByCurrency)    // 根据币种查询对应的汇率信息
		rateApi.GET("/rate_date_rate", RateService.GetDateRateList) // 查询多个币种的日期-汇率信息列表

		rateApi.GET("/raw", RateService.GetRawData)

		// -- 初始化汇率编码记录 -- 只允许运行一次
		rateApi.GET("/init", RateService.GetExcelSaveDb)
		// -- 自动化处理-- 只允许运行一次
		rateApi.GET("/auto", RateService.AutoDeal)

		// 初始化 2024年12月汇率
		rateApi.GET("/init2024", RateService.RequestDay)
	}

	router.GET("/exchangerates_data/timeseries", RateService.GetRateByDateTimeseries)

	err := router.Run(viper.GetString("server.httpPort"))
	if err != nil {
		fmt.Println("gin启动失败", err)
		return nil
	}
	return router
}
